define(['moment'], function (moment) {
    return ['$rootScope', '$scope', '$filter', '$stateParams', 'config', 'MMWService', '$timeout',
        function ($rootScope, $scope, $filter, $stateParams, config, $MMWService, $timeout) {
            $scope.init = function(){
                $scope.data = {
                    dispose_user: config.cache.account,
                    pcs_list:[]
                };
                $scope.barcode_no = '';
                $scope.item_list = [];
                document.getElementById('barcode_no').focus();
                document.getElementById('barcode_no').select();
            };

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        $scope.barcode_no = result.text;
                        input_barcode_no_callback();
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('B0214.showAlert.column3'));
                    },
                    orientation : 'landscape'
                });
            };

            // barcode輸入
            $scope.input_barcode_no = function(event){
                if(event.keyCode == 13){
                    $scope.search();
                }
            };

            // 查詢序號
            $scope.search = function (content){
                var barcode = $scope.barcode_no;
                $scope.barcode_no = '';
                if(!content){//add by Oliver for M#74913 要讓後端判斷是否用panel_no再走一次查號流程，故在此要做一個遞迴，第一次呼叫如有回傳recall==1，則要再呼叫一次
                    $scope.data = {};
                    $scope.hasRecall = false;
                    var content={
                        input_no:barcode,
                        type:'3'
                    }
                }
                if(content.input_no != ''){
                    $MMWService.sendToServer({
                        uri:'ServicesSMT.Module_SMT.panel_info_get',
                        content : content,
                        success: function(data){
                            if(+data.recall == 1){
                                if(data.pcs_no && data.panel_no){
                                    $scope.hasRecall = true;
                                    $scope.data.pcs_no = data.pcs_no;
                                    $scope.data.panel_no = data.panel_no;
                                }
                                this.content.input_no = data.panel_no;
                                this.content.recall = data.recall;
                                $scope.search(this.content);
                            }else{
                                if(data.pcs_list && data.pcs_list.length > 0){
                                    if(data.reason_list.length > 0){
                                        $scope.$root.showAlert($filter('translate')('SMT15.msg.seq_maintaining'),'error');
                                    } else {
                                        var _result = ValidatePCSStatus(data.status,data.pcs_list[0].pcs_no);
                                        if(_result.error_msg == ''){
                                            if(_result.warring_msg == ''){
                                                searchConfirm(data, barcode);
                                            } else {
                                                $scope.$root.showConfirm(_result.warring_msg, function(confirmDialog){
                                                    confirmDialog.hide();
                                                    searchConfirm(data, barcode);
                                                });
                                            }
                                        } else {
                                            $scope.$root.showAlert(_result.error_msg);
                                        }
                                    }
                                } else {
                                    $scope.data = {
                                        dispose_user: $scope.data.dispose_user,
                                        pcs_list:[]
                                    };
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                                }
                            }
                        }
                    });
                }
            }

            function searchConfirm(data, barcode) {
                $scope.data.barcode = barcode;
                $scope.data.pcs_list = data.pcs_list;
                $scope.data.mode = data.mode;
                $scope.data.machine_no = data.equipment_no;
                $scope.data.op_no = data.op_no;

                $scope.data.panel_no = data.panel_no;
                if(!$scope.hasRecall){
                    $scope.data.pcs_no = data.pcs_list[0].pcs_no;
                }
                $scope.data.plot_no = data.lot_no;
                $scope.data.product_no = data.product_no;
                $scope.data.item_spec = data.item_spec;
                $scope.data.qty = "1";//data.pcs_count;
                $scope.data.smt_op_seq = data.smt_op_seq;
            }

            // 選擇不良原因 (開窗)
            $scope.AddReason = function () {
                if($scope.data.pcs_list.length>0){
                    $MMWService.showQueryListPNIG({
                        page_mode:'tool_abnormal_reason_info_get',
                        key: 'reason_no',
                        header: [
                            {label:$filter('translate')('SMT15.list2.defect_reason_no'), code:'reason_no'},
                            {label:$filter('translate')('SMT15.list2.defect_reason_name'), code:'reason_name'}
                        ],
                        setCondition:function(conditions){
                            conditions.push({column_name:'ReasonType', query_mode:'0', value:'Product', merge_condition_model:'AND' });
                            return conditions;
                        },
                        confirm:function(selectItem, dialog){
                            var _temp = $filter('filter')($scope.item_list, {reason_no:selectItem.reason_no}, true);
                            if(_temp.length == 0){
                                dialog.hide();
                                $scope.item_list.push({
                                    reason_no:selectItem.reason_no,
                                    reason_name:selectItem.reason_name,
                                    defect_qty:1
                                });
                            } else {
                                $scope.$root.showAlert( $filter('translate')('SMT15.msg.reason_duplidate'));
                            }
                        }
                    });
                } else {
                    $scope.$root.showAlert( $filter('translate')('SMT15.msg.pcs_list_empty'));
                }
            };

            // 刪除不良原因項目
            $scope.removeRow = function (item) {
                $scope.item_list.splice($scope.item_list.indexOf(item), 1);
            };

            function ValidatePCSStatus(status,pcsNo){
                var _result = {error_msg : '', warring_msg:''};
                if(status && status != 'X'){
                    _result.warring_msg += $filter('translate')('SMT15.msg.seq_not_ng', pcsNo);
                }
                return _result;
            }

            // 確定送修
            $scope.confirm = function(){
                if($scope.item_list.length > 0 ){
                    $MMWService.sendToServer({
                        uri:'ServicesSMT.Module_SMT.panel_info_set_process',
                        content : {
                            op_no:$scope.data.op_no,
                            equipment_no:$scope.data.machine_no,
                            plot_no:$scope.data.plot_no,
                            panel_no:$scope.data.panel_no,
                            pcs_count: $scope.data.qty,
                            smt_op_seq: $scope.data.smt_op_seq,
                            rework_smt_op_seq: $scope.data.smt_op_seq,

                            dispose_user:$scope.data.dispose_user,
                            type:3,
                            mode:$scope.data.mode,
                            pcs_list:$scope.data.pcs_list,
                            reason_list:$scope.item_list,
                            pcs_no:$scope.data.pcs_no
                        },
                        success: function(data){
                            $scope.data = {
                                dispose_user: $scope.data.dispose_user,
                                pcs_list:[]
                            };
                            $scope.item_list = [];
                            $scope.$root.showToast($filter('translate')('SMT15.msg.maintaining_completed'));
                            document.getElementById('barcode_no').focus();
                            document.getElementById('barcode_no').select();
                        }
                    });
                } else {
                    $scope.$root.showAlert( $filter('translate')('SMT15.msg.reason_list_empty'));
                }
            }

            //选择工序
            $scope.loadSMTOPSeq = function () {
                if ($scope.data.plot_no != "") {
                    $MMWService.showQueryListPNIG({
                        page_mode: 'smt_op_seq_info_get',
                        key: 'smt_op_seq',
                        header: [
                            { label: $filter('translate')('SMT15.list3.smt_op_seq'), code: 'smt_op_seq' },
                            { label: $filter('translate')('SMT15.list3.smt_op_seq_name'), code: 'smt_op_seq_name' },
                            { label: $filter('translate')('SMT15.list3.description'), code: 'description' }
                        ],
                        setCondition: function (conditions) {
                            conditions.push({ column_name: 'c.BASELOTNO', query_mode: '0', value: $scope.data.plot_no, merge_condition_model: 'AND' });
                            return conditions;
                        },
                        confirm: function (selectItem, dialog) {
                            $scope.data.smt_op_seq = selectItem.smt_op_seq;
                        }
                    });
                } else {
                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                }
            };
        }
    ];
});
